package com.mzj.saas.mservice.property.repository;

import com.mzj.saas.mservice.property.entity.EstateExpenseBill;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import javax.persistence.OrderBy;
import java.util.List;

public interface EstateExpenseBillRepository extends PagingAndSortingRepository<EstateExpenseBill, Long>, JpaSpecificationExecutor<EstateExpenseBill>, JpaRepository<EstateExpenseBill, Long> {

    List<EstateExpenseBill> findByHouseIdAndPaymentStatusAndAuditStatus(String houseId, Integer paymentStatus, Integer auditStatus);

    List<EstateExpenseBill> findByIdIn(List<Long> ids);

    List<EstateExpenseBill> findByHouseId(String houseId);

    @OrderBy("create_time desc")
    List<EstateExpenseBill> findByRuleId(String ruleId);

    @OrderBy("create_time  asc")
    List<EstateExpenseBill> findByRuleIdAndHouseIdAndChargeType(String ruleId, String houseId, Integer chargeType);

    @Query(value = "SELECT * FROM saas_estate_expense_bill WHERE house_id = ?1 and  charge_type = ?2 and org_id = ?3 and audit_status = 1 and  payment_status != 1 ORDER BY last_update_time ", nativeQuery = true)
    List<EstateExpenseBill> findByHouseIdAndChargeTypeAndOrgId(String houseId, Integer chargeType, Long orgId);

    @Query(value = "SELECT * FROM saas_estate_expense_bill WHERE house_id = ?1  and org_id = ?2 and audit_status = 1 and  payment_status != 1 ORDER BY last_update_time ", nativeQuery = true)
    List<EstateExpenseBill> findByHouseIdAndOrgId(String houseId, Long orgId);

    @Query("select ee from EstateExpenseBill ee where ee.auditStatus = ?1 and  ee.paymentStatus in (?2,2)  and  ee.houseId = ?3 and ee.orgId = ?4 order by ee.createTime asc")
    List<EstateExpenseBill> findByAuditStatusAndPaymentStatusAndHouseIdAndOrgId(Integer auditStatus,Integer paymentStatus,String houseId,Long orgId);

    List<EstateExpenseBill> findByPaymentStatusAndHouseIdAndOrgId(Integer paymentStatus, String houseId, Long orgId);
}
